{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6774c8f6-b5ee-4389-bf48-9972a719f376",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import time\n",
    "import pandas as pd\n",
    "\n",
    "API_KEY = \"<YOUR_API_KEY>\"\n",
    "\n",
    "def is_erc20(address):\n",
    "    url = f\"https://api.etherscan.io/api\"\n",
    "    params = {\n",
    "        \"module\": \"contract\",\n",
    "        \"action\": \"getabi\",\n",
    "        \"address\": address,\n",
    "        \"apikey\": API_KEY\n",
    "    }\n",
    "    try:\n",
    "        response = requests.get(url, params=params, timeout=10).json()\n",
    "        abi = response.get(\"result\", \"\")\n",
    "        required = ['totalSupply', 'balanceOf', 'transfer']\n",
    "        return all(fn in abi for fn in required)\n",
    "    except Exception as e:\n",
    "        print(f\"Error for {address}: {e}\")\n",
    "        return False\n",
    "\n",
    "df_tokens_bigQuery = pd.read_csv(\"bigQuery_token_adds.csv\")\n",
    "token_addresses = df_tokens_bigQuery[\"token_address\"].values\n",
    "\n",
    "verified = []\n",
    "unverified = []\n",
    "for j,addr in enumerate(token_addresses):\n",
    "    if j%10==0:\n",
    "        print(\"iter: %d, num success: %d\" %(j,len(verified)))    \n",
    "    if is_erc20(addr):\n",
    "        verified.append(addr)\n",
    "    else:\n",
    "        unverified.append(addr)\n",
    "    time.sleep(0.1)  \n",
    "\n",
    "# Save result\n",
    "pd.DataFrame(verified, columns=[\"address\"]).to_csv(\"erc20_token_adds.csv\", index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99f77d4b-ab0d-4d1b-8715-456f5e691099",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
